package gui;


import java.util.ArrayList;

import javax.swing.table.DefaultTableModel;

import model.Customer;

public class CustomerTableModel extends DefaultTableModel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList<Customer> cList;
	
	public CustomerTableModel(ArrayList<Customer> c)
	{
		cList = c;
		showAll();
	}
	public CustomerTableModel(ArrayList<Customer> c, String criterier)
	{
		cList = c;
		searchCustomer(criterier);
	}
	
	private void showAll()
	{
		int counter = 0;
		setColumnCount(2);
		for(Customer c : cList) {
			setRowCount(counter +1);
			
			setValueAt(c.getName(), counter, 0);
			setValueAt(c.getPhone(), counter, 1);
			
			counter++;
		}
		if(counter == 0) {
			setRowCount(counter +1);
			
			setValueAt("Ingen resultat", counter, 0);
		}
	}
	
	private void searchCustomer(String criterie)
	{
		int counter = 0;
		setColumnCount(2);
		for(Customer c : cList) {
			if(c.getName().equals(criterie)) {
				setRowCount(counter +1);
				
				setValueAt(c.getName(), counter, 0);
				setValueAt(c.getPhone(), counter, 1);
				
				counter++;
			}
			else if(c.getPhone().equals(criterie)) {
				setRowCount(counter +1);
				
				setValueAt(c.getName(), counter, 0);
				setValueAt(c.getPhone(), counter, 1);
				
				counter++;
			}
			if(counter == 0) {
				setRowCount(counter +1);
				
				setValueAt("Ingen resultat", counter, 0);
			}
			
		}
	}
}
